package statusmachine;

public enum OrderState {
    PENDING_PAYMENT {
        @Override
        public OrderState pay() {
            System.out.println("订单已支付");
            return PAID;
        }
    },
    PAID {
        @Override
        public OrderState ship() {
            System.out.println("订单已发货");
            return SHIPPED;
        }
    },
    SHIPPED {
        @Override
        public OrderState deliver() {
            System.out.println("订单已送达");
            return DELIVERED;
        }
    },
    DELIVERED {
        // 终止状态
    };

    public OrderState pay() {
        throw new IllegalStateException("无法在此状态下执行支付操作");
    }

    public OrderState ship() {
        throw new IllegalStateException("无法在此状态下执行发货操作");
    }

    public OrderState deliver() {
        throw new IllegalStateException("无法在此状态下执行送达操作");
    }
}

// 使用示例
 class Order {
    private OrderState state = OrderState.PENDING_PAYMENT;

    public void pay() {
        state = state.pay();
        System.out.println("当前订单状态：" + state);
    }

    public void ship() {
        state = state.ship();
        System.out.println("当前订单状态：" + state);
    }

    public void deliver() {
        state = state.deliver();
        System.out.println("当前订单状态：" + state);
    }

    public OrderState getState() {
        return state;
    }

    public static void main(String[] args) {
        Order order = new Order();
        order.pay();
        order.ship();
        order.deliver();
    }

}
